package com.quasar.hpatient.view.friends;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.medicalmodel.FriendsData;
import com.quasar.hpatient.model.medicalmodel.SelectFriendsData;
import com.quasar.hpatient.onInterface.OnFriendListener;
import com.quasar.hpatient.presenter.NewsFriendPresenter;
import com.quasar.hpatient.view.adapter.AddFriendsAdapter;
import com.quasar.hpatient.widget.MyView;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

import zxing.activity.CaptureActivity;

/**
 * 添加好友
 */
@EActivity(R.layout.activity_add_friends)
public class AddFriendsActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener,
        OnFriendListener {

    private int delayMillis = 1000;//设置刷新等待的时间
    private  AddFriendsAdapter addFriendsAdapter;
    String phone;//记录输入的手机号码
    private NewsFriendPresenter newsFriendPresenter;


    //扫一扫
    @ViewById(R.id.mv_scan)
    MyView mv_scan;
    @ViewById(R.id.main_toolbar)
    Toolbar main_toolbar;
    @ViewById(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @ViewById(R.id.rv_list)
    RecyclerView rv_list;
    @ViewById(R.id.ll_search)
    LinearLayout ll_search;
    @ViewById(R.id.et_search)
    EditText et_search;

    @AfterViews
    void init(){
        initView();
    }
    @Override
    protected void initView() {
        initToolbar();
        initSearchEdittext();
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        rv_list.setLayoutManager(new LinearLayoutManager(this));
        initData();
    }

    @Override
    protected void initData() {
        newsFriendPresenter =new NewsFriendPresenter(this);
        initAdapter();
    }
    //初始化adapter布局
    public void initAdapter() {
        addFriendsAdapter = new AddFriendsAdapter();
        addFriendsAdapter.setOnLoadMoreListener(this, rv_list);
        addFriendsAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        rv_list.setAdapter(addFriendsAdapter);
        //item点击
        addFriendsAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                switch (view.getId()){
                    case R.id.tv_addto:
                        newsFriendPresenter.PostAddFriend(addFriendsAdapter.getData().get(position).getList().getId()+"");
                        break;
                }
            }
        });
    }
    //初始化搜索按钮
    public void initSearchEdittext(){
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    phone=et_search.getText().toString();
                    if(phone.length()>0){
                        newsFriendPresenter.searchFriend(phone);
                    }else{
                        msg("请输入好友账号");
                    }

                    return false;
                }

                return false;
            }
        });
    }

    @Override
    public void onRefresh() {
        ViseLog.d("onRefresh");
        newsFriendPresenter.searchFriend(phone);
        addFriendsAdapter.setEnableLoadMore(false);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //下拉后添加新数据
                swipeLayout.setRefreshing(false);//下拉刷新关闭
                addFriendsAdapter.setEnableLoadMore(true);
            }
        }, delayMillis);
    }

    @Override
    public void onLoadMoreRequested() {
        ViseLog.d("onLoadMoreRequested");
        addFriendsAdapter.loadMoreEnd();
    }

    //扫一扫
    private static final int REGISTERED = 100;
    @Click(R.id.mv_scan)
    public void mv_scan(){
        Intent intent = new Intent(this, CaptureActivity.class);
        startActivityForResult(intent, REGISTERED);
    }
    String phonesNum;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            if (resultCode == REGISTERED) {
                phonesNum = data.getStringExtra("iPhone");
                newsFriendPresenter.searchFriend(phonesNum);
            }
        }
    }


    @Override
    public void onSuccess(DataStateResult data) {

    }

    @Override
    public void onError(String msg) {
        if(msg!=null){
            msg(msg);
        }
    }

    @Override
    public void onRefreshUI() {
        addFriendsAdapter.notifyDataSetChanged();
    }

    @Override
    public void onNewsFriendSuccess(AnotherResult<FriendsData> data) {

    }

    @Override
    public void onNewsFriendRefreshUI(DataStateResult data) {

    }

    @Override
    public void searchFriend(SelectFriendsData selectFriendsData) {
        List<SelectFriendsData> list=new ArrayList<>();
        if(selectFriendsData.getList()!=null){
            list.add(selectFriendsData);
        }else{
            msg("没有找到该用户");
        }
        addFriendsAdapter.setNewData(list);
    }

    //添加好友
    @Override
    public void onAddtoFriend(DataStateResult dataStateResult) {

        if(dataStateResult.getMsgCode().equals("0x00000000")){
            String iphone=et_search.getText().toString().trim();
            msg("添加成功");
            if(iphone==null || iphone.length()==0){
                iphone= phonesNum;
            }
            newsFriendPresenter.searchFriend(iphone);
        }
    }
}
